package com.simple.datax;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.junit.Ignore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.simple.datax.api.MessageService;

/** 
 * @Title: Main.java 
 * @Package com.simple.datax 
 * @Description: TODO(添加描述) 
 * @author Paladin Chen 
 * @date 2013-1-10 上午11:27:08 
 * @version V1.0 
 */
public class Main {
	@Ignore
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "META-INF/spring/applicationContext*.xml" });
		
		//testIntraService(context);
	}
	
	private static void testIntraService(ApplicationContext context){
		MessageService ms = (MessageService)context.getBean("dataxService");
		
		String xmlfile = readFileByLines("F:/tmp/RPC.FLOW_c.XML");
		
		if (!xmlfile.startsWith("<")) {
			int beginIndex = xmlfile.indexOf("<");
			xmlfile = xmlfile.substring(beginIndex);
			System.out.println(beginIndex);
		}
		
		ms.service(xmlfile);
	}
	
	
	
	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static String readFileByLines(String fileName) {

		StringBuffer sb = new StringBuffer("");
		BufferedReader reader = null;
		try {
			// System.out.println("以行为单位读取文件内容，一次读一整行：");
			FileInputStream fis = new FileInputStream(fileName);
			InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
			reader = new BufferedReader(isr);
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				// System.out.println("line " + line + ": " + tempString);
				line++;

				sb.append(tempString).append("\r\n");
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return sb.toString();
	}
}
